package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.TaobaoCourtMainJframe;
import org.come.Jpanel.RechargeVIPGoodsJpanel.Mlistener;
import org.come.bean.LoginResult;
import org.come.bean.PayvipBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.RechargeVIPGoodsMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.SrcollPanelUI;
import org.come.until.UserMessUntil;

import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;

public class RechargeVIPJpanel extends JPanel {
    private JLabel labtext1, labtext2;// 
    
    private Map<String, String> map = new HashMap<>();
    
    public static List<Goodstable> GoodstableList = new ArrayList<>();
    private static Goodstable goodstable;
    
	/**
	 * 充值反馈礼包
	 */
	private List<RechargeVIPGoodsJpanel> goodsList;

	/**
	 * 滚动条
	 */
	private JScrollPane scrollPane;
	/**
	 * VIP等级
	 */
	int vipLvl = 0;
	
	private List<PayvipBean> payvipBeanList;
	
	/** 当前VIP进度 */
	private int langNum = 0;
	
	private ImageIcon iconTop;

	private ImageIcon iconBack;

	private ImageIcon iconPlan;

	private List<ImageIcon> iconVip;
	
	private static ImageIcon iconVipk;
	
	private int xz = -1;// 选中单位
	
	private int sxz = -1;// 鼠标滑过单位
	
	private static Mlistener mlistener ;
	
	private JLabel laba,labb,labc,labd;
	
	private JLabel[] GoodsListLabel = new JLabel[7];// 用来存放24个物品表格
	
    public RechargeVIPJpanel() {
    	
    	// 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/background/gift-close-btn.png", 1, 3002);
        offBtn.setBounds(683 - 28, 22, 19, 18);
        this.add(offBtn);
    	
        this.setPreferredSize(new Dimension(700, 419));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);
		this.setOpaque(false);
		getScrollPane();
		getGoodsList();
    }

    private ImageIcon icon = CutButtonImage.getImage("inkImg/old/background/S100vip.png", 700, 419);
    private ImageIcon icon1 = CutButtonImage.getImage("inkImg/old/background/日历底图1.png", 474, 319);
    private ImageIcon icon3 = CutButtonImage.getImage("inkImg/old/background/gift-item-border.png",67,52);


    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(icon.getImage(), 0, 0, 700, 419, this);
        
//        if (iconTop == null) {
//			iconTop = CutButtonImage.getImage("inkImg/old/background/S4.png", -1, -1);
//		}
//		g.drawImage(iconTop.getImage(), 18, 20, 591, 116, this);
//		if (iconBack == null) {
//			iconBack = CutButtonImage.getImage("inkImg/old/background/S5.png", -1, -1);
//		}
//		g.drawImage(iconBack.getImage(), 30, 70, 507, 7, this);
//		if (iconPlan == null) {
//			iconPlan = CutButtonImage.getImage("inkImg/old/background/S6.png", -1, -1);
//		}
//		g.drawImage(iconPlan.getImage(), 20, 121, langNum, 5, this);
		g.setFont(UIUtils.TEXT_FONT2);
		g.setColor(Color.black);
		g.drawString("累计积分:", 275, 280);
		g.setColor(Color.red);
		g.drawString(RoleData.getRoleData().getLoginResult().getMoney().toString()+" 积分",340, 280);
		g.setColor(Color.black);
		g.drawString("累计充值:", 495, 280);
		g.setColor(Color.red);
		g.drawString(RoleData.getRoleData().getLoginResult().getPaysum().toString()+" 元",560, 280);
		if(GoodstableList!=null && GoodstableList.size()>0){
			for(int i=0;i<GoodstableList.size();i++) {
				if(GoodstableList.get(i)!=null) {
					g.drawImage(icon3.getImage(), 40+i*65, 240, 60, 60, this);
					icon1 = CutButtonImage.getImage("img/item/" + GoodstableList.get(i).getSkin() + ".png", -1, -1);
					g.drawImage(icon1.getImage(), 40+i*65, 240, 60, 60, this);
				}
			}
		}
    }
    
    /**
	 * 刷新面板
	 */
	public void refreshPanel(){
		showGoodsList(payvipBeanList);
	}
	/**
	 * 获取数据
	 */
	public void getShop() {
		String sendmes = Agreement.getAgreement().GetvipgradepackAgreement();
		SendMessageUntil.toServer(sendmes);
	}
	/**
	 * 获取VIP礼包列表
	 * 
	 * @param payvipBeanList
	 */
	public void showGoodsList(List<PayvipBean> payvipBeanList) {
		this.payvipBeanList = payvipBeanList;
		LoginResult loginResul = RoleData.getRoleData().getLoginResult();
		BigDecimal paysum = loginResul.getPaysum();
		int maxLevel = 0;
		if (payvipBeanList.size() == 0) {
			for (int i = 0; i < goodsList.size(); i++) {
				goodsList.get(i).showGoods(null);
			}
		} else {
			String vipget = RoleData.getRoleData().getLoginResult().getVipget();
			String[] split = null;
			if(vipget!=null) {
				split = vipget.split("&&");
			}
			for (int i = 0; i < goodsList.size(); i++) {
				if (i > payvipBeanList.size()) {
					goodsList.get(i).showGoods(null);
				} else if (i < payvipBeanList.size()) {
					PayvipBean payvipBean = payvipBeanList.get(i);
					if (goodsList.size() < payvipBeanList.size()) {
						goodsList.add(new RechargeVIPGoodsJpanel());
					}
					if (paysum.compareTo(new BigDecimal(payvipBeanList.get(i).getPaynum())) >= 0) {
						vipLvl = vipLvl > payvipBean.getGrade() ? vipLvl : payvipBean.getGrade();
					}
					maxLevel = maxLevel > payvipBean.getGrade() ? maxLevel : payvipBean.getGrade();
					goodsList.get(i).showGoods(payvipBean);
					if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "1", payvipBean.getGrade() + "")) {
						goodsList.get(i).getVipShopBtn().setBtn(-1);
						goodsList.get(i).getVipShopBtn().setIcon(CutButtonImage.getImage("inkImg/old/background/gift-received-btn.png", -1, -1));
					} else {
						goodsList.get(i).getVipShopBtn().setBtn(1);
						try {
							goodsList.get(i).getVipShopBtn().setIcons(CutButtonImage.cuts("inkImg/old/background/gift-receive-btn.png"));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			langNum = getLangNum(maxLevel, paysum);
		}
		addGoods();
	}
	public int getLangNum(Integer gradeInteger, BigDecimal paysum) {
		BigDecimal divide = getVipLevel(paysum).divide(new BigDecimal(gradeInteger), 3, BigDecimal.ROUND_HALF_UP);
		BigDecimal multiply = divide.multiply(new BigDecimal(587)).setScale(0, BigDecimal.ROUND_DOWN);
		return langNum = (multiply.intValue() > 587 ? 587 : multiply.intValue());
	}
    public BigDecimal getVipLevel(BigDecimal paysum) {
		BigDecimal level = new BigDecimal(0);
		if (paysum.compareTo(new BigDecimal(30)) < 0) {
			level = new BigDecimal(0);
		} else if (paysum.compareTo(new BigDecimal(100)) < 0) {
			level = new BigDecimal(1);
		} else if (paysum.compareTo(new BigDecimal(500)) < 0) {
			level = new BigDecimal(2);
		} else if (paysum.compareTo(new BigDecimal(1000)) < 0) {
			level = new BigDecimal(3);
		} else if (paysum.compareTo(new BigDecimal(2000)) < 0) {
			level = new BigDecimal(4);
		} else if (paysum.compareTo(new BigDecimal(5000)) < 0) {
			level = new BigDecimal(5);
		} else if (paysum.compareTo(new BigDecimal(10000)) < 0) {
			level = new BigDecimal(6);
		} else if (paysum.compareTo(new BigDecimal(18000)) < 0) {
			level = new BigDecimal(7);
		} else if (paysum.compareTo(new BigDecimal(30000)) < 0) {
			level = new BigDecimal(8);
		} else if (paysum.compareTo(new BigDecimal(50000)) < 0) {
			level = new BigDecimal(9);
		} else if (paysum.compareTo(new BigDecimal(50000)) >= 0) {
			level = new BigDecimal(10);
		}
		return level;
	}
    /**
	 * 展示滚动条物品
	 */
	public void addGoods() {
		JPanel view = (JPanel) scrollPane.getViewport().getView();
		view.removeAll();
		view.repaint();
		int num = 0;
		for (int i = 0; i < goodsList.size(); i++) {
			
			RechargeVIPGoodsJpanel rechargeVIPGoodsJpanel = goodsList.get(i);
			if (rechargeVIPGoodsJpanel.getPayvipBean() == null) {
				continue;
			}
			rechargeVIPGoodsJpanel.setBounds(200 + num % 3 * 115, num / 3 * 155, 136, 155);
			num++;
			view.add(rechargeVIPGoodsJpanel);
		}
		view.setPreferredSize(new Dimension(765, 155 * (num % 3 == 0 ? num / 3 : num / 3 + 1)));
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();
		TaobaoCourtMainJframe.getTaobaoCourtJframe().getTaobaoCourtMainJpanel().getTaobaoCourtCardJpanel().getCardLayout()
				.show(TaobaoCourtMainJframe.getTaobaoCourtJframe().getTaobaoCourtMainJpanel().getTaobaoCourtCardJpanel(), "vipshop");
	}
	
	public List<RechargeVIPGoodsJpanel> getGoodsList() {
		if (goodsList == null) {
			goodsList = new ArrayList<>();
			for (int i = 0; i < 15; i++) {
				RechargeVIPGoodsJpanel rechargeVIPGoodsJpanel = new RechargeVIPGoodsJpanel();
				goodsList.add(rechargeVIPGoodsJpanel);
			}
			for (int i = 0; i < 7; i++) {
				GoodsListLabel[i] = new JLabel();
                GoodsListLabel[i].addMouseListener(new RechargeVIPGoodsMouslisten(i));
                GoodsListLabel[i].setBounds(40+i*65, 240, 60, 60);
                this.add(GoodsListLabel[i]);
			}
		}
		return goodsList;
	}
	
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			// 属性滚动条
			scrollPane = new JScrollPane();
			scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
			scrollPane.getVerticalScrollBar().setUnitIncrement(155);
			scrollPane.getViewport().setOpaque(false);
			scrollPane.setOpaque(false);
			scrollPane.setBounds(58, 85, 590, 155);
			scrollPane.setBorder(BorderFactory.createEmptyBorder());
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			JPanel jPanel = new JPanel();
			jPanel.setBackground(null);
			jPanel.setOpaque(false);
			jPanel.setBorder(null);
			jPanel.setLayout(null);
			scrollPane.setViewportView(jPanel);
			this.add(scrollPane);
		}
		return scrollPane;
	}
	
//	private ImageIcon icon2;
//	
//	public void paintPeople(Graphics g,int x,int y,int t,int d) {
//		icon2 = new ImageIcon("inkImg/old/background/S163.png");
//		g.drawImage(icon2.getImage(), x,y,t,d, this);
//	}
	
	
	
	public static void paintPeopleGoods(PayvipBean payvipBean) {
		GoodstableList = new ArrayList<>();
		String str = payvipBean.getGivegoods();
		String[] goods = str.split("&");
		if(goods.length>0) {
			for(int i=0;i<goods.length-1;i++) {
				String goodId = goods[i+1].split("\\$")[0];
				Goodstable goodstable = UserMessUntil.getgoodstable(new BigDecimal(goodId));
				showGoods(goodstable);
				GoodstableList.add(goodstable);
			}
		}
	}
	
	
	/**
	 * 展示物品
	 * 
	 * @param
	 */
	public static void showGoods(Goodstable goodstables) {
		goodstable = goodstables;
		if (goodstable != null) {
			getIconVipK().setImage(CutButtonImage.getImage("img/item/" + goodstables.getSkin() + ".png", -1, -1).getImage());
		}
	}
	
	
	
	/** 展示 */
	public void upSXz(int v) {
		this.sxz = v;
	}

	public void upXz(int v) {
		this.xz = v;
	}
	
	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public static ImageIcon getIconVipK() {
		if (iconVipk == null) {
			iconVipk = new ImageIcon();
		}
		return iconVipk;
	}

	public void setIconVip(ImageIcon iconVipk) {
		RechargeVIPJpanel.iconVipk = iconVipk;
	}
	public Mlistener getMlistener() {
		return mlistener;
	}

	public void setMlistener(Mlistener mlistener) {
		RechargeVIPJpanel.mlistener = mlistener;
	}
}
